<?php
class CommonAction extends Action
{
    public $siteInfo = array();
    public function initCommon()
    {
        header("content-type:text/html; charset=utf-8;");
        defined('__RELATEID__') or die('您可能还没有绑定域名');
        defined('__SITEID__') or die('找不到这个站点');
		
		$site = M('Site')->where(array('id' => __SITEID__))->find();

        
        $this->siteInfo = $site;
        if (!$site['status'] > 0) {
            $this->error('该站点被禁用');
        }
        if (intval($site['timeout']) < intval(time())) {
			//查找代理站点
			$agentsite = M('Site') -> where(array('id' => $this->siteInfo['agentid'])) -> find();
			if($agentsite){
				//如果平台绑定了域名，使用域名打开
				if($agentsite['domain'] != ''){
					$agentsitedomain = explode(',', $agentsite['domain']);
					//产品已经到期，提醒用户续费
					$this->agentsitedomain = $agentsitedomain;
					$this->display('Public:timeout');
					
				}else{
					$agentsitedomain = C('DOMAIN_MAIN');
					$this->agentsitedomain = $agentsitedomain[0];
					$this->display('Public:timeout');
				}
				exit;
			}
            $this->error('该站点已经过期');
        }
		$this->memberInfo = session('memberInfo');
        //dump(checkmobile());exit;
        if ($this->siteInfo['mobileUrl'] != '' && checkmobile()) {
            header("location:" . $this->siteInfo['mobileUrl']);
            exit;
        }if(isset($_GET['dump'])){dump(__RELATEID__);}
        $siteurl = C('DOMAIN_MAIN');
        $this->siteurl = $siteurl[0];
        //多站点AFF_ID错误
        if (isset($_COOKIE['aff_id'])) {
			
        }
        if (isset($_GET['aff_id']) && intval($_GET['aff_id']) > 0) {
            $id = intval($_GET['aff_id']);
            $this->aff_id = $id;
        }
        $member = M('Member')->where(array('site_id' => __RELATEID__, 'id' => intval($this->aff_id)))->find();
        if (!$member) {
            $this->aff_id = 0;
        } else {
            setcookie('aff_id', $this->aff_id, time() + 300, '/');
        }

    } 

    public function _initialize()
    {
        //引入积分基类
        import("@.ORG.Integrals.Integral");
        $this->initCommon();
if($_SERVER['HTTP_HOST'] == 'www.ydzh99.com' && $this->is_weixin()){
			    redirect('http://m.ydzh99.com'.__SELF__);
			}

        if ($this->is_weixin() && $this->siteInfo['is_wxLogin'] > 0) {
			
            //dump(session('memberInfo'));exit;
			
			$memberInfo = session('memberInfo');
            if (!session('memberInfo') || (session('memberInfo') && $memberInfo['site_id'] != __RELATEID__)) {
                if (session('memberInfo')) {
                    session('memberInfo',NULL);
                }
                Vendor('Weixin.Weixin');
                $d['token'] = getSysVal('token');
                $d['appid'] = getSysVal('appid'); 
                $d['appsecret'] = getSysVal('appsecret');
				
                $this->weixin = new WeixinChat($d);
                if ($this->siteInfo['is_wxLogin'] == 1) {
                    $snsapi_base = 1;
                } elseif ($this->siteInfo['is_wxLogin'] == 2) {
                    $snsapi_base = 0;
                }
		
               
                $this->weixin->redirectGetOauthCode('http://' . $_SERVER['HTTP_HOST'] . __APP__ . "/Member/Public/getcode.html", $snsapi_base, intval($this->aff_id) . '_and_' . base64_encode(__SELF__));
            }

        }
		weixinshare_log($this->aff_id);
		if(defined('__PAGERELATEID__')){
            $this->assign(getLangConf(__PAGERELATEID__));
		}else{
			$this->assign(getLangConf(__SITEID__));
		}
        $this->_Common();
        //读取公用头尾模块
    }
	
	
	 
	//单独获取一个模块
	public function getModulesByID($view,$ModulesDB){
		if(isset($_GET['modulesID']) && intval($_GET['modulesID']) > 0){
			$ret['status'] = 1;
			$ret['id'] = intval($_GET['modulesID']);
			$ret['data'] = $ModulesDB -> getModulesSource(intval($_GET['modulesID']), $view);
			//分页和数据
			$ret['tbody'] = $ret['data'];
			$ret['pages'] = $view -> get('pages');
			$ret['dump'] = $_REQUEST;
			exit(json_encode($ret));
		}
	}
	
	
    public function getCommomModules($view = '')
    {
        $ModulesDB = D('Modules');

        $headerModules = $ModulesDB->getModulesList(__SITEID__, 'header', $view->get());

        $footerModules = $ModulesDB->getModulesList(__SITEID__, 'footer', $view->get());

        $this->assign('headerModules', $headerModules);
        $this->assign('footerModules', $footerModules);
    }

    //生成静态数据缓存
    public function _Common()
    {
        if (F('Common')) {
            $this->assign(F('Common'));
        } else {
            $cateModel = D('Category');
            $newsModel = D('News');

            $goodsCategoryModel = D('Product_category');
            $_Common['goodsCategory'] = $goodsCategoryModel->where(array('site_id' => __RELATEID__, 'status' => 1))->order('sort, id desc')->select();
            //推荐产品
            $_Common['recProduct'] = $newsModel->__list('n', 'c', 'm', "n.status=1 and n.is_rec=1 and m.name='Product' and n.site_id=" . __RELATEID__, "n.sort asc,n.id desc,n.is_top desc");
            //热门产品
            $_Common['hotProduct'] = $newsModel->__list('n', 'c', 'm', "n.status=1 and n.is_hot=1 and m.name='Product' and c.site_id=" . __RELATEID__ . " and n.site_id=" . __RELATEID__, " n.sort asc,n.id desc,n.is_top desc");
            //最新产品
            $_Common['newProduct'] = $newsModel->__list('n', 'c', 'm', "n.status=1 and m.name='Product' and c.site_id=" . __RELATEID__ . " and n.site_id=" . __RELATEID__, "n.datetime desc");

            //最新阅读
            $_Common['newArticle'] = $newsModel->__list('n', 'c', 'm', "n.status=1 and m.name='News' and c.site_id=" . __RELATEID__ . " and n.site_id=" . __RELATEID__, "n.datetime desc");
            //热门产品
            $_Common['hotArticle'] = $newsModel->__list('n', 'c', 'm', "n.status=1 and n.is_hot=1 and m.name='News' and c.site_id=" . __RELATEID__ . " and n.site_id=" . __RELATEID__, "n.sort asc,n.is_top desc");
            //推荐阅读
            $_Common['recArticle'] = $newsModel->__list('n', 'c', 'm', "n.status=1 and n.is_rec=1 and m.name='News' and c.site_id=" . __RELATEID__ . " and n.site_id=" . __RELATEID__, "n.sort asc,n.is_top desc");
            //生成所有分类列表
            $allCategory = $cateModel->alias('c')->join("left join __MODULE__ as m on m.id=c.module_id")->Field("m.name as module_name,c.*")->where(array('c.status' => 1, 'c.site_id' => __RELATEID__))->order('c.sort')->select();
            $news_extends = M('News_extends');
            foreach ($allCategory as $k => $v) {
                $ids = implode(',', $cateModel->getAllIDBypid($v['id'], __RELATEID__));
                $v['list'] = $newsModel->__list('n', 'c', 'm', "n.status=1 and n.category_id in({$ids}) and c.site_id=" . __RELATEID__ . " and n.site_id=" . __RELATEID__, "n.is_top desc,n.id desc,n.sort asc");
                $allCategory[$k]['list'][] = $v['list'];

                /*图组和扩展信息*/
                foreach ($v['list'] as $kk => $vv) {
                    if (!empty($vv['mimg_url'])) {
                        $v['list'][$kk]['mimg_url'] = explode(',', $vv['mimg_url']);

                    }
                    $extends = $news_extends->where(array('news_id' => $vv['id']))->select();
                    $exdata = array();
                    foreach ($extends as $kkk => $vvv) {
                        $exdata[$vvv['name']] = $vvv['val'];
                    }
                    $v['list'][$kk]['extends'] = $exdata;
                }
                /*图组和扩展结束*/

                $v['subCate'] = $cateModel->where(array('pid' => $v['id'], 'status' => 1))->order('sort')->select();
                if ($v['alias_name'] != '') {
                    $_Common[$v['alias_name']] = $v;
                    //已关联的栏目子类
                }
                $_Common[$v['name']] = $v;
            }
            //页面渲染
            F('Common', $_Common);
            $this->assign($_Common);
        }
    }


    //判断是否为微信
    public function is_weixin()
    {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
            return true;
        }
        return false;
    }
}

?>